
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:keep_account/utils/selection_radio_widget.dart';
import 'package:keep_account/vm/ymd_work_records_controller.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:keep_account/bean/entry.dart';

class YearMonthDayWorkRecords extends StatefulWidget {
  const YearMonthDayWorkRecords({super.key});
  @override
  State<StatefulWidget> createState() => _YearMonthDayWorkRecordsState();

}

class _YearMonthDayWorkRecordsState extends State<YearMonthDayWorkRecords> {

  late final YMDWorkRecordsController _controller = Get.put(YMDWorkRecordsController());

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight
    ]);
    return Scaffold(
      body: _controller.obx((state){
        return SafeArea(child: SizedBox(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          child: Stack(
            fit: StackFit.loose,
            alignment: Alignment.topCenter,
            children: <Widget>[
              Container(
                height: double.infinity,
                padding: const EdgeInsets.only(top: 10,bottom: 0,left: 10,right: 20),
                child: Obx(() => SfCartesianChart(
                  primaryXAxis: CategoryAxis(
                      isVisible: true
                  ),
                  selectionType: SelectionType.series,
                  zoomPanBehavior: ZoomPanBehavior(
                      enableMouseWheelZooming: true,
                      enableSelectionZooming: true,
                      zoomMode: ZoomMode.xy,
                      enablePanning: true,
                      enablePinching: true
                  ),
                  legend: Legend(
                      isVisible: true,
                      position: LegendPosition.top,
                      alignment: ChartAlignment.far,
                      toggleSeriesVisibility: true,
                      overflowMode: LegendItemOverflowMode.wrap
                  ),
                  trackballBehavior: TrackballBehavior(
                      lineType: TrackballLineType.vertical,
                      enable: true,
                      tooltipSettings: const InteractiveTooltip(
                          color: Colors.purple,
                          connectorLineColor: Colors.yellow
                      ),
                      tooltipAlignment: ChartAlignment.center,
                      shouldAlwaysShow: true,
                      tooltipDisplayMode: TrackballDisplayMode.floatAllPoints
                  ),
                  tooltipBehavior: TooltipBehavior(
                      enable: true,
                      shared: true,
                      activationMode: ActivationMode.singleTap
                  ),
                  series: [
                    ColumnSeries(
                        name: "工资",
                        width: .1,
                        dataSource: _controller.salaryEntry.value,
                        xValueMapper: (en,_) => en.workDate,
                        yValueMapper: (en,_) => en.salary,
                        enableTooltip: true
                    )
                  ],
                )),
              ),
              Positioned(
                  width: 35,
                  height: 35,
                  left:  10,
                  bottom:  10,
                  child: Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.zero,
                    width: double.infinity,
                    height: double.infinity,
                    decoration: const BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                        color: Colors.grey
                    ),
                    child: IconButton(
                      padding: EdgeInsets.zero,
                      onPressed: (){ Get.back();},
                      icon: const Icon(Icons.arrow_back_ios_new,color: Colors.white,),
                    ),
                  )
              ),
              Positioned(
                width: 200,
                height: 40,
                top: 10,
                child: SelectionRadio(
                  defaultIndex: 1,
                  controller: _controller.selectionRadioController,
                ),
              )
            ],
          ),
        ));
      },
        onEmpty:Stack(
          fit: StackFit.expand,
          children: <Widget>[
            const Center(
              child: Text("未找到数据或网络错误，请检查网络或稍后重试"),
            ),
            Positioned(
                width: 35,
                height: 35,
                left:  10,
                bottom:  10,
                child: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.zero,
                  width: double.infinity,
                  height: double.infinity,
                  decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                      color: Colors.grey
                  ),
                  child: IconButton(
                    padding: EdgeInsets.zero,
                    onPressed: (){ Get.back();},
                    icon: const Icon(Icons.arrow_back_ios_new,color: Colors.white,),
                  ),
                )
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown
    ]);
    super.dispose();
  }


}